var util = require('../../utils/util.js');
// pages/bindPhone/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked: true,
    curPhone: '',
    myCode: "",
    isSend: false,
    btntext: '获取验证码',
    codev: '',
  },

  savePhone: function (e) {
    var phone = e.detail.value;
    this.setData({
      curPhone: phone
    })
  },

  inputCode(e) {
    this.setData({
      myCode: e.detail.value
    })
  },

  findPhone() {
    var that = this;
    // 对手机号码进行验证
    if (this.data.curPhone.length == 11 && this.data.curPhone.indexOf('1') == 0) {
      if (!this.data.isSend) {
        let data = {
          mobile: this.data.curPhone,
          event: 'register'
        }
        util.http({
          url: 'api/miniwechat/user/sendSms',
          method: 'POST',
          data: data
        }).then((res) => {
          var restTimeStamp = new Date().getTime() + 60 * 1000;
          wx.setStorageSync('REST_COUNT_DOWN', restTimeStamp);
          countDown_60s(that, restTimeStamp);
          wx.showToast({
            title: String("发送成功"),
            icon: 'none',
            duration: 2000
          })
        }).catch((err) => {
          wx.showToast({
            title: String(err),
            icon: 'none',
            duration: 2000
          })
        })
      }
    } else {
      wx.showToast({
        title: "请输入正确手机号！",
        icon: 'none',
        duration: 2000
      })
    }
  },

  confrimBind() {
    let that =this;
    if (this.data.curPhone.length != 11 || this.data.curPhone.indexOf('1') != 0) {
      wx.showToast({
        title: "请输入正确手机号！",
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (this.data.myCode === "" || this.data.myCode === null) {
      wx.showToast({
        title: "请输入验证码！",
        icon: 'none',
        duration: 2000
      })
      return false
    }
    var data = {
      user_id: util.isLogin().user_id || this.data.userId, 
      mobile: this.data.curPhone,
      captcha: this.data.myCode
    }
    util.http({
      url: 'api/miniwechat/user/bind',
      method: 'POST',
      data: data
    }).then((res) => {
      if (res.bind_uid == 0) {
        wx.showToast({
          title: String("绑定成功"),
          icon: 'none',
          duration: 2000
        })
        if (this.data.userId) {
          wx.getStorage({
            key: 'backUserInfo',
            success(res) {
              res.data.mobile = that.data.myCode
              wx.setStorage({
                key: 'userInfo',
                data: res.data
              })
              wx.removeStorage({
                key: 'backUserInfo',
                success(res) {
                }
              })
            }
          })
          setTimeout(function () {
            wx.reLaunch({
              url: '/pages/index/index'
            })
          }, 1000)
        } else {
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })
          }, 1000)
        }
      } else {
        wx.showToast({
          title: String("绑定成功，请重新登录！"),
          icon: 'none',
          duration: 1000
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/index/index?id=4'
          })
        }, 1000)
      }
    }).catch((err) => {
      wx.showToast({
        title: String(err),
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.userId) {
      this.data.userId = options.userId;
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

function countDown_60s(_this, restTimeStamp) {
  var nowtimeStamp = new Date().getTime();
  var coden = parseInt((restTimeStamp - nowtimeStamp) / 1000); // 定义60秒的倒计时
  if (coden > 0) {
    _this.setData({
      isSend: true
    })
    _this.data.codeV = setInterval(function () {
      _this.setData({
        btntext: (--coden) + 's'
      })
      if (coden < 0) { // 清除setInterval倒计时，这里可以做很多操作，按钮变回原样等
        clearInterval(_this.data.codeV);
        wx.removeStorageSync('REST_COUNT_DOWN');
        _this.setData({
          isSend: false,
          btntext: '重新发送'
        })
      }
    }, 1000) //  1000是1秒
  } else {
    _this.setData({
      isSend: false
    })
  }
}